package presentation.optionForm_UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import business.optionForm_Evt.Controls_Evt;

public class ControlsForm_UI extends JDialog {

        private JButton btSelect = new JButton("Space");
        private JButton btSubmit = new JButton("Enter");
        private JButton btClear = new JButton("BackSpace");
        private JButton btUp = new JButton("↑");
        private JButton btDown = new JButton("↓");
        private JButton btLeft = new JButton("←");
        private JButton btRight = new JButton("→");
        private JButton btRestore = new JButton("Reset to Default");
        private JButton btSave = new JButton("Save");
        private JButton btCancel = new JButton("Cancel");
        
        private JButton[] listButton = new JButton[]{btUp, btDown, btLeft, btRight, btSelect, btSubmit, btClear};
        
        private JPanel panelMain = new JPanel();
        private JPanel panelControlsButton = new JPanel();
        private JPanel panelIntro = new JPanel();
        private JPanel panelButton = new JPanel();
        
        private JTextArea taIntro = new JTextArea();
        private JScrollPane scroll = new JScrollPane();
        
        private Controls_Evt event = new Controls_Evt(this);
        
        private static final long serialVersionUID = 1L;
        private final int keyUp = KeyEvent.VK_UP;
        private final int keyDown = KeyEvent.VK_DOWN;
        private final int keyLeft = KeyEvent.VK_LEFT;
        private final int keyRight = KeyEvent.VK_RIGHT;
        private final int selectKey = KeyEvent.VK_SPACE;
        private final int submitKey = KeyEvent.VK_ENTER;
        private final int clearKey = KeyEvent.VK_BACK_SPACE;
        private int[] listKey = new int[7];

        public ControlsForm_UI(JDialog owner) {
                super(owner, "Controls", true);
                setMainLayout();
                
                buttonEvent();
                keyEvent();
                
                listKey = event.loadCustomControlKeys(btUp, btDown, btLeft, btRight, btSelect, btSubmit, btClear);
                event.setListButton(listButton);
                this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                this.setLocationRelativeTo(owner);
                this.setResizable(false);
        }

        private void setMainLayout() {
                panelMain.setLayout(null);

                panelMain.add(panelButton);
                panelMain.add(panelControlsButton);
                panelMain.add(panelIntro);

                panelIntro.setBounds(10, 10, 550, 80);
                panelControlsButton.setBounds(panelIntro.getX(), panelIntro.getY() + panelIntro.getHeight() + 5, panelIntro.getWidth(), 155);
                panelButton.setBounds(panelIntro.getX(), panelControlsButton.getY() + panelControlsButton.getHeight() + 5, panelIntro.getWidth(), 60);

                panelControlsButton.setBorder(BorderFactory.createTitledBorder(""));
                panelButton.setBorder(BorderFactory.createTitledBorder(""));

                this.setLayout(null);
                this.add(panelMain);
                panelMain.setBounds(0, 0, 25 + panelIntro.getWidth(), 55 + panelIntro.getHeight() + panelButton.getHeight() + panelControlsButton.getHeight());
                this.setSize(panelMain.getWidth(), panelMain.getHeight());

                setPanelIntroLayout();
                setPanelButtonLayout();
                setPanelControlsButtonLayout();
        }

        private void setButtons() {
                btSelect.setFont(new Font("Comic Sans MS", 0, 16));
                btSelect.setToolTipText("Use space button to choose word!");
                btSelect.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));

                btSubmit.setFont(new Font("Comic Sans MS", 0, 16));
                btSubmit.setToolTipText("Use enter button to commit the word!");
                btSubmit.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));

                btClear.setFont(new Font("Comic Sans MS", 0, 16));
                btClear.setToolTipText("Use BackSpace button to Clear the word!");
                btClear.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));

                btUp.setFont(new Font("Arial", 0, 16));
                btUp.setToolTipText("Use up arrow button to move up!");
                btUp.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));

                btDown.setFont(new Font("Arial", 0, 16));
                btDown.setToolTipText("Use down arrow button to move down!");
                btDown.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));

                btLeft.setFont(new Font("Arial", 0, 16));
                btLeft.setToolTipText("Use left arrow button to move left!");
                btLeft.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));

                btRight.setFont(new Font("Arial", 0, 16));
                btRight.setToolTipText("Use right arrow button to move right!");
                btRight.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));

                btSave.setFont(new Font("Arial", 0, 16));
                btSave.requestFocus();
                btSave.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));

                btCancel.setFont(new Font("Arial", 0, 16));
                btCancel.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));

                btRestore.setFont(new Font("Arial", 0, 16));
                btRestore.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));
        }

        private void setPanelIntroLayout() {
                panelIntro.setBorder(BorderFactory.createTitledBorder("Introduction"));
                panelIntro.setLayout(new BorderLayout());
                scroll.setViewportView(taIntro);
                taIntro.setEditable(false);
                taIntro.setFocusable(false);
                panelIntro.add(scroll, BorderLayout.CENTER);
        }

        private void setPanelControlsButtonLayout() {
                setButtons();
                panelControlsButton.setLayout(null);
                panelControlsButton.add(btClear);
                panelControlsButton.add(btUp);
                panelControlsButton.add(btDown);
                panelControlsButton.add(btLeft);
                panelControlsButton.add(btRight);
                panelControlsButton.add(btSubmit);
                panelControlsButton.add(btSelect);

                btUp.setBounds(85, 25, 50, 50);
                btLeft.setBounds(btUp.getX() - 60, btUp.getY() + btUp.getHeight() + 10, btUp.getWidth(), btUp.getHeight());
                btDown.setBounds(btLeft.getX() + 10 + btLeft.getWidth(), btLeft.getY(), btUp.getWidth(), btUp.getHeight());
                btRight.setBounds(btDown.getX() + 10 + btDown.getWidth(), btLeft.getY(), btUp.getWidth(), btUp.getHeight());

                btSubmit.setBounds(btRight.getX() + btRight.getWidth() + 20, btUp.getY(), btUp.getWidth() + 100, btUp.getHeight());
                btClear.setBounds(btSubmit.getX() + btSubmit.getWidth() + 10, btUp.getY(), btSubmit.getWidth(), btUp.getHeight());
                int gap = btClear.getX() - (btSubmit.getX() + btSubmit.getWidth());
                btSelect.setBounds(btSubmit.getX(), btSubmit.getY() + btSubmit.getHeight() + 10, btSubmit.getWidth() + btSubmit.getWidth() + gap, btSubmit.getHeight());
        }

        private void setPanelButtonLayout() {
                panelButton.setLayout(null);
                panelButton.add(btSave);
                panelButton.add(btCancel);
                panelButton.add(btRestore);

                btRestore.setBounds(25, 10, 170, 40);
                btSave.setBounds(btRestore.getX() + 20 + btRestore.getWidth(), btRestore.getY(), 150, btRestore.getHeight());
                btCancel.setBounds(btSave.getX() + 10 + btSave.getWidth(), btRestore.getY(), btSave.getWidth(), btRestore.getHeight());
        }

        private void buttonEvent() {

                btCancel.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                                event.btCancel_Evt();
                        }
                });

                btSave.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                        	setNewKeyLists();
                        	int choice = JOptionPane.showConfirmDialog(null, "Do you want to save your change...", "Message", JOptionPane.YES_NO_OPTION);
                        	if(choice == JOptionPane.YES_OPTION)
                        	{
                                event.updateControlKeys(listKey[0], listKey[1], listKey[2], listKey[3], listKey[4], listKey[5], listKey[6]);
                                JOptionPane.showMessageDialog(null, "Your change have been changed...","Message",JOptionPane.INFORMATION_MESSAGE);
                        	}
                        }
                });

                btRestore.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                        	restoreKey();
                        }
                });

                btUp.addActionListener(new ActionListener() {
                	
                	@Override
                	public void actionPerformed(ActionEvent e) {
                		event.changeKeyEvent(btUp);
                	}
                });
                
                btDown.addActionListener(new ActionListener() {
                	
                	@Override
                	public void actionPerformed(ActionEvent e) {
                		event.changeKeyEvent(btDown);
                	}
                });
                btLeft.addActionListener(new ActionListener() {
                	
                	@Override
                	public void actionPerformed(ActionEvent e) {
                		event.changeKeyEvent(btLeft);
                	}
                });
                btRight.addActionListener(new ActionListener() {
                	
                	@Override
                	public void actionPerformed(ActionEvent e) {
                		event.changeKeyEvent(btRight);
                	}
                });
                btSelect.addActionListener(new ActionListener() {
                	
                	@Override
                	public void actionPerformed(ActionEvent e) {
                		event.changeKeyEvent(btSelect);
                	}
                });
                btSubmit.addActionListener(new ActionListener() {
                	
                	@Override
                	public void actionPerformed(ActionEvent e) {
                		event.changeKeyEvent(btSubmit);
                	}
                });
                btClear.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						event.changeKeyEvent(btClear);
					}
				});
                
        }
        
        private void restoreKey()
        {
        	for(int i = 0; i<listButton.length; i++)
        	{
        		listButton[i].setText(event.defaultKeys[i]);
        	}
        	event.updateControlKeys(keyUp, keyDown, keyLeft, keyRight, selectKey, submitKey, clearKey);
//        	event.updateControlKeys(listKey[0], listKey[1], listKey[2], listKey[3], listKey[4], listKey[5], listKey[6]);
        }
        
        private void setNewKeyLists()
        {
        	for(int i = 0; i<listButton.length; i++)
        	{
        		listKey[i] = event.convertKey(listButton[i].getText());
        	}
        }

        private void keyEvent() {
                // ================================
                // Button up
                // ================================
                btUp.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btRestore.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btDown.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btClear.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btSubmit.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                // ================================
                // Button down
                // ================================
                btDown.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btRestore.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btUp.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btLeft.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btRight.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                // ================================
                // Button left
                // ================================
                btLeft.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btRestore.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btUp.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btSelect.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btDown.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                // ================================
                // Button Right
                // ================================
                btRight.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btRestore.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btUp.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btDown.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btSelect.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                // ================================
                // Button Submit
                // ================================
                btSubmit.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btSave.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btSelect.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btUp.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btClear.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                // ================================
                // Button Clear
                // ================================
                btClear.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btSelect.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btRestore.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btSubmit.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btUp.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                // ================================
                // Button Select
                // ================================
                btSelect.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btSave.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btSubmit.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btRight.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btLeft.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                // ================================
                // Button Restore
                // ================================
                btRestore.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btUp.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btDown.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btCancel.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btSave.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                // ================================
                // Button Save
                // ================================
                btSave.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btSubmit.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btSelect.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btRestore.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btCancel.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                // ================================
                // Button Cancel
                // ================================
                btCancel.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btSubmit.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btSelect.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btSave.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btRestore.requestFocus();
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });
        }

}
